const Koa = require('koa')
const app = new Koa()
const Router = require('koa-router')
let router = new Router()

const mongoose = require('mongoose')
const fs = require('fs')

// 地址信息
router.get('/insertAllAddress', async (ctx) => {
  fs.readFile('./data_json/Address.json', 'utf8', (err, data) => {
    let saveCount = 0
    const Address = mongoose.model('Address')
    data = JSON.parse(data)
    data.RECORDS.map((value, index) => {
      let newGoods = new Address(value)
      newGoods
        .save()
        .then(() => {
          saveCount++
          console.log('成功' + saveCount)
        })
        .catch((error) => {
          console.log('失败：' + error)
        })
    })
  })
  ctx.body = '开始导入数据'
})


// 购物车信息
// 插入所有购物车信息
router.get('/insertCartGoodsInfo', async (ctx) => {
  fs.readFile('./data_json/Cart.json', 'utf8', (err, data) => {
    let saveCount = 0
    const Cart = mongoose.model('Cart')
    data = JSON.parse(data)
    data.RECORDS.map((value, index) => {
      let newGoods = new Cart(value)
      newGoods
        .save()
        .then(() => {
          saveCount++
          console.log('成功' + saveCount)
        })
        .catch((error) => {
          console.log('失败：' + error)
        })
    })
  })
  ctx.body = '开始导入数据'
})

// 插入所有商品信息
router.get('/insertAllGoodsInfo', async (ctx) => {
  fs.readFile('./data_json/goods.json', 'utf8', (err, data) => {
    let saveCount = 0
    const Goods = mongoose.model('Goods')
    data = JSON.parse(data)
    data.RECORDS.map((value, index) => {
      let newGoods = new Goods(value)
      newGoods
        .save()
        .then(() => {
          saveCount++
          console.log('成功' + saveCount)
        })
        .catch((error) => {
          console.log('失败：' + error)
        })
    })
  })
  ctx.body = '开始导入数据'
})

// 插入所有的分类信息
router.get('/insertAllCategoryInfo', (ctx) => {
  // 数据读取过来
  // 转化为对象的形式
  // 每个数据遍历 然后.save()
  fs.readFile('./data_json/Category.json', 'utf-8', (err, data) => {
    let newData = JSON.parse(data)
    let Category = mongoose.model('Category')
    newData.RECORDS.map((value, index) => {
      try {
        value = new Category(value)
        let res = value.save()
        console.log(res, '写入成功')
      } catch (error) {
        console.log(error, '写入失败')
      }
    })
  })
  ctx.body = '开始导入大分类数据'
})

// 插入所有的父级分类信息
router.get('/insertAllCategorySub', async (ctx) => {
  fs.readFile('./data_json/category_sub.json', 'utf8', (err, data) => {
    data = JSON.parse(data)
    let saveCount = 0
    const CategorySub = mongoose.model('CategorySub')
    data.RECORDS.map((value, index) => {
      console.log(value)
      let newCategorySub = new CategorySub(value)
      newCategorySub
        .save()
        .then(() => {
          saveCount++
          console.log('成功插入' + saveCount)
        })
        .catch((error) => {
          console.log('插入失败:' + error)
        })
    })
  })
  ctx.body = '开始导入数据'
})

// 查询商品大分类信息
router.get('/getCategoryList', async (ctx) => {
  try {
    let Category = mongoose.model('Category')
    let res = await Category.find().exec()
    ctx.body = {
      data: { code: 200, message: res },
    }
  } catch (error) {
    ctx.body = { code: 500, message: error }
  }
})
// 读取小类别的分类信息
router.post('/getCategorySubList', async (ctx) => {
  try {
    let CategorySub = mongoose.model('CategorySub')
    let CategoryId = ctx.request.body.catgoryId
    let res = await CategorySub.find({ MALL_CATEGORY_ID: CategoryId }).exec()
    ctx.body = {
      code: 200,
      data: {
        message: res,
      },
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: {
        message: error,
      },
    }
  }
})

// 查询商品的详情信息
router.post('/getDetailGoodsInfo', async (ctx) => {
  let goodsId = ctx.request.body.goodsId
  let Goods = mongoose.model('Goods')
  try {
    console.log(goodsId, '我是goodsId');
    let res = await Goods.findOne({ _id: goodsId }).exec()
    console.log(res, '查询成功，')
    ctx.body = {
      code: 200,
      message: res,
    }
  } catch (error) {
    console.log(error, '查询失败')
    ctx.body = {
      code: 500,
      message: error,
    }
  }
})

// 根据商品子类的类别id获取商品列表（获取商品的信息 子类下面有哪些商品）
router.post('/getGoodsListByCategorySubID', async (ctx) => {
  try {
    let page = ctx.request.body.page
    let num = 10
    let start = (page - 1) * 10
    let Goods = mongoose.model('Goods')
    let res = await Goods.find().skip(start).limit(num).exec()
    ctx.body = {
      data: {
        code: 200,
        message: res,
      },
    }
  } catch (error) {
    ctx.body = {
      code: 500,
      data: {
        message: error,
      },
    }
  }
})

module.exports = router
